﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class CancelPurchase : Form
    {
        private int purchaseID;
        public CancelPurchase()
        {
            InitializeComponent();
        }

        public CancelPurchase(int purchaseID)
        {
            InitializeComponent();
            this.purchaseID = purchaseID;
            onload(purchaseID);
        }

        private void onload(int purchaseID)
        {
            PurchaseDao dao = new PurchaseDao();
            DataRow row = dao.getPurchase(purchaseID);
            if (row != null)
            {
                lblPurchaseName.Text = row["NAME"] == null? "" : row["NAME"].ToString();
                lblDate.Text = row["PURCHASE_DATE"] == null ? "" : DateTime.Parse(row["PURCHASE_DATE"].ToString()).ToShortDateString();
                lblSothang.Text = row["TOTAL_MONTH"] == null ? "" : row["TOTAL_MONTH"].ToString();
                lblTotal.Text = row["TOTAL_AMOUN"] == null ? "" : row["TOTAL_AMOUN"].ToString();
            }
        }
        private void CancelPurchase_Load(object sender, EventArgs e)
        {

        }

        private void lblDate_Click(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            PurchaseDao dao = new PurchaseDao();
            if (txtNotes.Text.Trim().Equals(""))
            {
                Error er = new Error(UserConstant.CANCEL_PURCHASE_INPUT);
                er.ShowDialog();
                er.Dispose();
            }
            else
            {
                if (dao.cancelPurchase(purchaseID, txtNotes.Text.Trim()))
                {
                    Result r = new Result(UserConstant.CANCEL_PURCHASE_SUC);
                    r.ShowDialog();
                    r.Dispose();
                    this.Dispose();
                }else
                {
                    Error er = new Error(UserConstant.CANCEL_PURCHASE_FAIL);
                    er.ShowDialog();
                    er.Dispose();
                }
            }
        }
    }
}
